package cn.itcast.recordAction;
import cn.itcast.domain.Results;
import cn.itcast.domain.HeiMaClass;
import cn.itcast.domain.Record;
import cn.itcast.recordservice.RecordService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class RecordAction extends ActionSupport implements ModelDriven<Record> {

    @Autowired
    private RecordService recordService;


    private Record record = new Record();

    @Override
    public Record getModel() {
        return record;
    }
    /**
     *记录保存
     */
    @Action(value="record_save")
    public void save(){
        recordService.save( record);
    }


    /**
     *按月份查询
     */
    private String  month = new String ();

    public void setDate(String  month) {
        this.month = month;
    }

    @Action(value="record_findByMonth",results = {@Result(name = "success",type = "json")})
    public String findByMonth(){
        List<Record> records = recordService.findByMonth(month);
        List<HeiMaClass> classes = new ArrayList<>();
        for(Record re: records){
            HeiMaClass h = recordService.findByCid(re.getCid());
            classes.add(h);
        }
       List<Results>  r = new ArrayList<>();
        for (int i=0;i<records.size();i++){
            r.get(i).setClassName(classes.get(i).getClassName());
            r.get(i).setCount(classes.get(i).getCount());
            r.get(i).setRoommate(classes.get(i).getRoommate());
            r.get(i).setClassStatus(classes.get(i).getClassStatus());
            r.get(i).setRid(records.get(i).getRid());
            r.get(i).setTimeBucket(records.get(i).getTimeBucket());
            r.get(i).setRequiredCount(records.get(i).getRequiredCount());
            r.get(i).setActualCount(records.get(i).getActualCount());
            r.get(i).setLateStudent(records.get(i).getLateStudent());
            r.get(i).setVacateStudent(records.get(i).getVacateStudent());
            r.get(i).setSlipperNum(records.get(i).getSlipperNum());
            r.get(i).setSleepNum(records.get(i).getSleepNum());
            r.get(i).setPhoneNum(records.get(i).getPhoneNum());
            r.get(i).setSanitation(records.get(i).getSanitation());
            r.get(i).setLearnAtmosphere(records.get(i).getLearnAtmosphere());
            r.get(i).setRemark(records.get(i).getRemark());
            r.get(i).setShiftTeacher(records.get(i).getShiftTeacher());
            r.get(i).setInputDate(records.get(i).getInputDate());
            r.get(i).setAttendance(records.get(i).getAttendance());

        }


        ActionContext.getContext().getValueStack().push(r);
        return SUCCESS;
    }
    /**
     *按日期查询
     */
    private String  day = new String ();

    public void setDay(String day) {
        this.day = day;
    }
    @Action(value="record_findByDay",results = {@Result(name = "success",type = "json")})
    public String findByDay(){
        Record records = recordService.findByDay(day);
         HeiMaClass classes=recordService.findByCid(records.getCid());
         Results r  = new Results();
        r.setClassName(classes.getClassName());
        r.setCount(classes.getCount());
        r.setRoommate(classes.getRoommate());
        r.setClassStatus(classes.getClassStatus());
        r.setRid(records.getRid());
        r.setTimeBucket(records.getTimeBucket());
        r.setRequiredCount(records.getRequiredCount());
        r.setActualCount(records.getActualCount());
        r.setLateStudent(records.getLateStudent());
        r.setVacateStudent(records.getVacateStudent());
        r.setSlipperNum(records.getSlipperNum());
        r.setSleepNum(records.getSleepNum());
        r.setPhoneNum(records.getPhoneNum());
        r.setSanitation(records.getSanitation());
        r.setLearnAtmosphere(records.getLearnAtmosphere());
        r.setRemark(records.getRemark());
        r.setShiftTeacher(records.getShiftTeacher());
        r.setInputDate(records.getInputDate());
        r.setAttendance(records.getAttendance());

        ActionContext.getContext().getValueStack().push(r);
        return SUCCESS;
    }
}
